//
//  RichTypeTableViewCell.swift
//  CodeLibrary_Swift
//
//  Created by 陆文宇 on 2017/5/27.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

protocol RichTypeTableViewCellEditing: NSObjectProtocol {
    
    func richCell(cell: RichTypeTableViewCell, style: RichTypeTableViewCell.EditingStyle)
    
}

class RichTypeTableViewCell: UITableViewCell {
    
    enum EditingStyle {
        case add;
        case move;
        case delete
    }
    
    weak var controller: RichTypeTableViewCellEditing?
    
    var richType: RichType {
        set {
            switch newValue {
            case .none:
                richLabel.isHidden = true
                richImageView.isHidden = true
            case .txt:
                richLabel.isHidden = false
                richImageView.isHidden = true
            case .image:
                richLabel.isHidden = true
                richImageView.isHidden = false
            case .video:
                richLabel.isHidden = true
                richImageView.isHidden = true
            case .shop:
                richLabel.isHidden = true
                richImageView.isHidden = true
            case .ticket:
                richLabel.isHidden = true
                richImageView.isHidden = true
            }
        }
        
        get {
            if !richLabel.isHidden {
                return .txt
            }
            if !richImageView.isHidden {
                return .image
            }
            if !richLabel.isHidden {
                return .video
            }
            if !richLabel.isHidden {
                return .shop
            }
            if !richLabel.isHidden {
                return .ticket
            }
            return .none
        }
    }
    
    @IBOutlet weak var richLabel: UILabel!
    @IBOutlet weak var richImageView: UIImageView!
    @IBOutlet weak var addView: UIView!
    @IBOutlet weak var upView: UIView!
    @IBOutlet weak var delView: UIView!
    @IBOutlet weak var stackViewHeight: NSLayoutConstraint!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        richLabel.layer.borderColor = UIColor.lightGray.cgColor
        richLabel.layer.borderWidth = 0.5
        
        let add = UITapGestureRecognizer(target: self, action: #selector(add(_:)))
        let up = UITapGestureRecognizer(target: self, action: #selector(up(_:)))
        let del = UITapGestureRecognizer(target: self, action: #selector(del(_:)))
        
        addView.addGestureRecognizer(add)
        upView.addGestureRecognizer(up)
        delView.addGestureRecognizer(del)
    }
    
    @objc func add(_ sender: Any) {
        controller?.richCell(cell: self, style: .add)
    }
    
    @objc func up(_ sender: Any) {
        controller?.richCell(cell: self, style: .move)
    }
    
    @objc func del(_ sender: Any) {
        controller?.richCell(cell: self, style: .delete)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
